package com.fcy.teamwork.business.self

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.fcy.teamwork.pojo.JobApplyDetail
import com.fcy.teamwork.pojo.JobDetail
import com.fcy.teamwork.pojo.Person
import com.fcy.teamwork.repo.RetrofitClient
import kotlinx.coroutines.launch

class SelfInfoVM : ViewModel() {
    private var person: Person? = null
    private val service: SelfInfoService by lazy { RetrofitClient.getService() }
    private lateinit var userName: String
    fun initUserName(uName: String) {
        userName = uName
    }

    suspend fun loadMineInfo(): Person {
//        val person = service.loadPersonInfo(userName).data[0]
//        this.person = person
//        return person
        person = service.loadPersonInfo(userName).data[0]
        return person!!
    }

    suspend fun loadRelativeProject(target: String = userName): List<JobApplyDetail> {
        return service.loadProjectsInvolved(target).data
    }

    fun addSkill(skill: String) {
        person?.apply {
            skills = if (skills.isBlank()) {
                skill
            } else {
                "$skills,$skill"
            }
            viewModelScope.launch {
                service.updateSkill(this@apply)
            }
        }
    }
}